#!/bin/bash
# EasyDrop 服务卸载脚本
# 文件名: undeploy.sh

# ===== 配置参数（与部署脚本一致）=====
SERVER_IP="150.158.107.119"
SERVER_USER="ubuntu"
SERVER_PORT=22
SERVER_DIR="/opt/easydrop"
SYSTEMD_SERVICE="easydrop.service"

# ===== 卸载流程 =====
set -e

echo "=========================================="
echo "        🚨 EasyDrop 服务卸载脚本 🚨"
echo "=========================================="
echo "目标服务器: $SERVER_USER@$SERVER_IP"
echo "部署目录:   $SERVER_DIR"
echo "服务名:     $SYSTEMD_SERVICE"
echo "------------------------------------------"
read -p "⚠️  确认要卸载并永久删除上述服务及所有相关文件吗？[y/N]: " confirm
if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
    echo "已取消卸载操作。"
    exit 0
fi

echo -e "\n===== 开始卸载 EasyDrop 服务 ====="

# 停止并禁用服务
ssh -p $SERVER_PORT $SERVER_USER@$SERVER_IP \
    "sudo systemctl stop $SYSTEMD_SERVICE || true && \
     sudo systemctl disable $SYSTEMD_SERVICE || true && \
     sudo rm -f /etc/systemd/system/$SYSTEMD_SERVICE && \
     sudo systemctl daemon-reload"

# 删除部署目录
ssh -p $SERVER_PORT $SERVER_USER@$SERVER_IP \
    "sudo rm -rf $SERVER_DIR"

# 清理残留配置文件
ssh -p $SERVER_PORT $SERVER_USER@$SERVER_IP \
    "sudo rm -rf /var/log/easydrop.log || true"

echo -e "\n✅ 卸载完成! 所有相关文件和服务已清除"
echo "------------------------------------------"
echo "⚠️  警告：本操作不可逆！以下内容已被永久删除："
echo "1. 服务程序: $SERVER_DIR"
echo "2. 系统服务: /etc/systemd/system/$SYSTEMD_SERVICE"
echo "3. 日志文件: /var/log/easydrop.log"
echo "=========================================="